// index.js
import {
    get,
    post,
    put
} from '../../utils/request'
import {
    APIS
} from '../../utils/api'
Page({
    data: {
        id: '',
        action: 'new',
        currShop: {},
        tableNumbers: [],
        selectData: [],
        feeForm: [['00:00', '24:00']],
        cnNum: ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'],
        start: [0, 0],
        end: [24, 0],
        loading: false
    },
    onLoad(options) {
        let currShop = wx.getStorageSync('currShop')
        if (options.shop) {
            const shopList = wx.getStorageSync('shopList')
            currShop = shopList.find(s => +s.id === +options.shop)
        }
        this.setData({
            currShop,
            action: options.action || 'new',
            id: options.id
        })
        if (options.action === 'modify') {
            this.getDetail();
        }
        this.getTables()
    },
    handleTime(e) {
        let {
            feeForm
        } = this.data;
        let {
            pos = 0,
            type,
            value
        } = e.detail;
        let tmp = feeForm[pos] || ['00:00', '24:00'];
        tmp[type === 'start' ? 0 : 1] = value;
        feeForm[pos] = tmp;
        this.setData({
            feeForm
        })
    },
    getTables() {
        get(APIS.rulesegment + '/tables', {
            shop: this.data.currShop.id
        }).then(res => {
            const list = res.data;
            this.setData({
                tableNumbers: list
            });
        })
    },
    handleSelect(e) {
        const {
            type,
            val
        } = e.target.dataset;
        const {
            selectData
        } = this.data;
        let idx = selectData
            .findIndex(v => v.type === type && v.id === val.id)
        if (idx > -1) {
            this.setData({
                selectData: selectData.filter((_, i) => i !== idx)
            })
        } else {
            this.setData({
                selectData: [
                    ...selectData,
                    {
                        type,
                        id: val.id
                    }
                ]
            })
        }
    },
    getDetail() {
        get(`${APIS.rulesegment}/${this.data.currShop.id}/${this.data.id}`)
            .then(res => {
                if (res.code === 1) {
                    wx.showToast({
                        title: res.message,
                        icon: 'none'
                    })
                    return;
                }
                let data = res.data;
                let selectData = [
                    ...data.table.cards.map(v => ({
                        type: 'card',
                        ...v
                    })),
                    ...data.table.balls.map(v => ({
                        type: 'ball',
                        ...v
                    }))
                ];
                console.log(data.table.cards, 'adf')
                this.setData({
                    name: data.name,
                    deposit: data.deposit,
                    amount: data.fee[0].fee,
                    selectData,
                    feeForm: [data.fee[0].range],
                    start: data.fee[0].range[0]
                        .split(':')
                        .map(v => parseInt(v)),
                    end: data.fee[0].range[1]
                        .split(':')
                        .map(v => parseInt(v))
                })
            })
    },
    formSubmit(e) {
        let {
            amount,
            deposit,
            name
        } = e.detail.value;
        if (!(amount && deposit && name)) {
            wx.showToast({
                title: '缺少必填项',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (this.data.loading) {
            return;
        }
        this.setData({
            loading: true
        })
        const {
            action,
            feeForm,
            id,
            selectData,
            currShop
        } = this.data;
        post(APIS.rulesegment, {
            name,
            deposit,
            shop: currShop.id,
            fee: [{
                fee: amount,
                range: feeForm[0]
            }],
            action,
            table: selectData,
            id
        }).then(res => {
            this.setData({
                loading: false
            })
            wx.showToast({
                title: res.message,
                icon: 'none'
            })
            wx.navigateBack({
                delta: 1
            })
        })
    }
})